<!DOCTYPE HTML>
<html>
<head>
  <title>Notification Basics</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="MockServices.js"></script>
  <script type="text/javascript" src="NotificationTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script type="text/javascript">

  SimpleTest.requestFlakyTimeout("untriaged");

  function deleteAllNotifications(done) {
    var promise = Notification.get();
    promise.then(function (notifications) {
      notifications.forEach(function(notification) {
        notification.close();
      });
      done();
    });
  }

  var info = NotificationTest.info;

  var steps = [
    function (done) {
      info("Test that Notifcation.get fulfills the promise");
      var promise = Notification.get();
      ok(promise.then, "should return a promise");

      // Create a new notification to make sure
      // Notification.get() works while creating
      var notification = new Notification("this is a test");

      promise.then(function () {
        ok(true, "promise should be fulfilled");
        done();
      });
    },

    deleteAllNotifications,

    function (done) {
      info("Test adding a notification, and making sure get returns it");
      NotificationTest.allowNotifications();
      var options = NotificationTest.payload;

      var notification = new Notification("This is a title", options);
      var promise = Notification.get();
      promise.then(function (notifications) {
        ok(notifications.length, "should return notifications");
        for (var i = 0; i < notifications.length; i++) {
          var notification = notifications[i];
          if (notification.tag === options.tag) {
            ok(true, "should contain newly created notification");
            for (var key in options) {
              if (key === "data") {
                ok(NotificationTest.customDataMatches(notification.data),
                   "data property should match");
                continue;
              }
              is(notification[key], options[key], key + " property should match");
            }
            notification.close();
            return;
          }
        }
        ok(false, "should contain newly created notification");
        notification.close();
      });
      notification.onclose = done;
    },

    function (done) {
      info("Testing fetching notification by tag filter");
      var n1 = new Notification("title1", {tag: "tag1"});
      var n2 = new Notification("title2", {tag: "tag2"});
      var n3 = new Notification("title3", {tag: "tag3"});
      var promise = Notification.get({tag: "tag3"});
      promise.then(function (notifications) {
        var notification = notifications[0];
        is(notifications.length, 1, "should return 1 notification");
        is(notifications[0].title, "title3", "titles should match");
        is(notifications[0].tag, "tag3", "tags should match");
        var closeCount = 0;
        var waitForAll = function () {
          if (++closeCount >= 3) {
            done();
          }
        };
        n1.onclose = waitForAll;
        n2.onclose = waitForAll;
        n3.onclose = waitForAll;
        n1.close();
        n2.close();
        n3.close();
      });
    },

    deleteAllNotifications,

    function (done) {
      info("Testing fetching no notifications");
      var promise = Notification.get();
      promise.then(function (notifications) {
        is(notifications.length, 0, "should return 0 notifications");
        done();
      });
    },

    function (done) {
      info("Testing fetching multiple notifications");
      var n1 = new Notification("title1");
      var n2 = new Notification("title2");
      var n3 = new Notification("title3");
      var promise = Notification.get();
      promise.then(function (notifications) {
        is(notifications.length, 3, "should return 3 notifications");
        n1.close();
        n2.close();
        n3.close();
        done();
      });
    },

    deleteAllNotifications,

    function (done) {
      info("Testing 'alertfinished' removes the notification from DB");
      var n = new Notification("test-title" + Math.random());
      n.onclose = function() {
        Notification.get().then(function(notifications) {
          is(notifications.length, 0, "should return 0 notifications");
          done();
        });
      };
      info("Installing 'onshow' for " + n.title);
      n.onshow = function() {
        info("Triggered 'onshow' for " + n.title);
        window.setTimeout(function() {
          NotificationTest.fireCloseEvent(n.title);
        }, 100);
      };
    }
  ];

  MockServices.register();
  NotificationTest.run(steps, function () {
    MockServices.unregister();
  });
</script>
</body>
</html>
